﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data.Common;
using System.Data;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class PollData
    {
        public static int CreatePollQuestion(string question, int answerCount)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreatePollQuestion";
            dataMiner.AddParameter(command, "@Question", DbType.String, -1, ParameterDirection.Input, question);
            dataMiner.AddParameter(command, "@AnswerCount", DbType.Int32, 0, ParameterDirection.Input, answerCount);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static int CreatePollAnswer(string answer, int displayOrder, int questionId)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreatePollAnswer";
            dataMiner.AddParameter(command, "@Answer", DbType.String, -1, ParameterDirection.Input, answer);
            dataMiner.AddParameter(command, "@DisplayOrder", DbType.Int32, 0, ParameterDirection.Input, displayOrder);
            dataMiner.AddParameter(command, "@QuestionId", DbType.Int32, 0, ParameterDirection.Input, questionId);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static int QuestionTextCount(string question)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_QuestionTextCount";
            dataMiner.AddParameter(command, "@Question", System.Data.DbType.String, -1, System.Data.ParameterDirection.Input, question);
            count = dataMiner.GetOneData(command);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static DataTable GetAllQuestions()
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetAllPollQuestion";
            return dataMiner.GetListData(command);
        }

        public static int DeleteQuestions(List<int> questionIdsToDelete)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeletePollQuestions";
            dataMiner.AddParameter(command, "@QuestionIds", DbType.String, -1, ParameterDirection.Input, DataMiner.FormatXMLForIdArray(questionIdsToDelete));
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static DataTable GetPollQuestionById(int id)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPollQuestionById";
            dataMiner.AddParameter(command, "@QuestionId", DbType.Int32, 0, ParameterDirection.Input, id);
            return dataMiner.GetListData(command);
        }

        public static int CreatePollEvent(int questionId, DateTime startDateTime, DateTime endDateTime)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreatePollEvent";
            dataMiner.AddParameter(command, "@QuestionId", DbType.Int32, 0, ParameterDirection.Input, questionId);
            dataMiner.AddParameter(command, "@StartDateTime", DbType.DateTime, 0, ParameterDirection.Input, startDateTime);
            dataMiner.AddParameter(command, "@EndDateTime", DbType.DateTime, 0, ParameterDirection.Input, endDateTime);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static int PollEventDateOverlapCount(DateTime startDateTime, DateTime endDateTime)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_PollEventDateOverlapCount";
            dataMiner.AddParameter(command, "@StartDateTime", System.Data.DbType.DateTime, 0, System.Data.ParameterDirection.Input, startDateTime);
            dataMiner.AddParameter(command, "@EndDateTime", System.Data.DbType.DateTime, 0, System.Data.ParameterDirection.Input, endDateTime);
            count = dataMiner.GetOneData(command);

            return (int)CommonUtility.FilterNull(count, typeof(int));
        }

        public static DataTable GetPollAnswerByQuestionId(int questionId)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPollAnswerByQuestionId";
            dataMiner.AddParameter(command, "@QuestionId", DbType.Int32, 0, ParameterDirection.Input, questionId);
            return dataMiner.GetListData(command);
        }
    }
}
